#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define N 10
void RunChild()
{
    int cnt = 5;
    while(cnt)
 {
        printf("I am Child Process, pid: %d, ppid:%d\n", getpid(), getppid());
        sleep(1);
        cnt--;
    }
}

     int main()
    {
        pid_t id = fork();
        if(id == 0)
        {
            RunChild();
            exit(0);
        }
        printf("create child process: %d success\n", id); // 这句话只有父进程才会执行
    

    //sleep(10);

    // 等待
    for(int i = 0; i < N; i++)
    {
        //pid_t id = wait(NULL);
        int status = 0;
        pid_t id = waitpid(-1, &status, 0);
        if(id > 0)
        {
            printf("wait %d success, exit code: %d\n", id, WEXITSTATUS(status));
        }
    }

   }

